package com.tanhua.app.controllerservice;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.api.db.BlackListApi;
import com.tanhua.api.db.QuestionApi;
import com.tanhua.api.db.SettingsApi;
import com.tanhua.domain.db.Question;
import com.tanhua.domain.db.Settings;
import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.vo.PageBeanVo;
import com.tanhua.domain.vo.SettingsVo;
import com.tanhua.domain.vo.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Service
public class SettingsService {

    @Reference
    private QuestionApi questionApi;

    @Reference
    private SettingsApi settingsApi;

    @Reference
    private BlackListApi blackListApi;


    public ResponseEntity findSettings() {

        User user = UserHolder.getUser();
        // 根据id查询问题对象
        Question question = questionApi.findQuestion(user.getId());
        // 根据id查询通知对象
        Settings settings = settingsApi.findSettings(user.getId());
        // 将问题和通知设置个settingsVo
        SettingsVo vo = new SettingsVo();
        if(question!=null){
            vo.setStrangerQuestion(question.getStrangerQuestion());
        }
        if(settings!=null){
            vo.setLikeNotification(settings.getLikeNotification());
            vo.setPinglunNotification(settings.getPinglunNotification());
            vo.setGonggaoNotification(settings.getGonggaoNotification());
        }
        vo.setPhone(user.getPhone());
        vo.setId(user.getId());

        // 返回settingsVo给app
        return ResponseEntity.ok(vo);

    }

    public ResponseEntity saveOrUpdateQuestions(String content) {

        // 根据id查询问题
        Long userId = UserHolder.getUser().getId();
        Question question = questionApi.findQuestion(userId);
        if(question==null){
            // 没有-新增
            question=new Question();
            question.setStrangerQuestion(content);
            question.setUserId(userId);
            questionApi.save(question);
        }else{
            // 有-修改
            question.setStrangerQuestion(content);
            questionApi.update(question);
        }
        return ResponseEntity.ok(null);
    }

    public ResponseEntity saveOrUpdateTZ(Settings settings) {

        // 根据id查询通知
        Long userId = UserHolder.getUser().getId();
        Settings st = settingsApi.findSettings(userId);
        if(st==null){
            // 没有-新增
            settings.setUserId(userId);
            settingsApi.save(settings);
        }else{
            // 有-修改
            st.setPinglunNotification(settings.getPinglunNotification());
            st.setLikeNotification(settings.getLikeNotification());
            st.setGonggaoNotification(settings.getGonggaoNotification());
            settingsApi.update(st);
        }
        return ResponseEntity.ok(null);

    }

    public ResponseEntity findBlackList(Integer page, Integer pagesize) {
        //分页查询
        IPage<UserInfo> iPage = blackListApi.findBlackList(page, pagesize, UserHolder.getUser().getId());
        //将iPage的内容转换成前端要的pageBean
        PageBeanVo pageBeanVo = new PageBeanVo(page,pagesize,iPage.getTotal(),iPage.getRecords());
        return ResponseEntity.ok(pageBeanVo);
    }

    public ResponseEntity deleteBlackById(Integer blackId, Long userId) {

        blackListApi.deleteBlackById(blackId,userId);

        return ResponseEntity.ok(null);
    }
}
